package archimede;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Properties;
import java.util.Scanner;
import java.util.StringTokenizer;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.UIManager;

import net.miginfocom.swing.MigLayout;

import com.itextpdf.text.BadElementException;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;

public class Premesse extends JPanel {

	private JPanel descr = new JPanel();

	private JTextPane descrizioneField = new JTextPane();

	private JPanel photo = new JPanel();
	private JButton upload1 = new JButton(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
	private JButton upload2 = new JButton(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
	private JButton upload3 = new JButton(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
	private JButton upload4 = new JButton(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));

	private JPanel dati = new JPanel();
	private JTextField tempEsterno = new JTextField() {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};
	private JTextField tempInterno = new JTextField() {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};
	private JTextField tempInterrato = new JTextField() {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};
	private JTextField umidEsterno = new JTextField() {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};
	private JTextField umidInterno = new JTextField() {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};
	private JTextField umidInterrato = new JTextField() {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};
	private JLabel umAssEsterno = new JLabel("   calcola") {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};
	private JLabel umAssInterno = new JLabel("   calcola") {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};
	private JLabel umAssInterrato = new JLabel("   calcola") {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};
	private double[] tabella = { 3.24, 3.52, 3.81, 4.13, 4.47, 4.84, 5.20, 5.6, 6, 6.4, 6.8, 7.3, 7.8, 8.3, 8.8, 9.4, 10, 10.7, 11.4, 12.1, 12.8,
			13.6, 14.5, 15.4, 16.3, 17.3, 18.3, 19.4, 20.6, 21.80, 23, 24.4, 25.8, 27.2, 28.7, 30.3 };

	private JTextPane condizioniMeteo = new JTextPane();

	private String lastPath = "";
	private String pathIm1 = "";
	private String pathIm2 = "";
	private String pathIm3 = "";
	private String pathIm4 = "";
	private String sourceIm1 = "";
	private String sourceIm2 = "";
	private String sourceIm3 = "";
	private String sourceIm4 = "";
	private String sufixIm1 = "";
	private String sufixIm2 = "";
	private String sufixIm3 = "";
	private String sufixIm4 = "";

	String directoryProject;

	protected void paintComponent(Graphics g) {
		setOpaque(false);
		g.drawImage((new ImageIcon(ClassLoader.getSystemResource("sfondo.jpg"))).getImage(), 0, 0, null);
		super.paintComponent(g);
	}

	public Premesse(final StartingWindow st) {
		super();
		setLayout(new MigLayout("", "", "25[]"));
		Properties pc = System.getProperties();
		String homePath = pc.getProperty("user.home");
		directoryProject = homePath + pc.getProperty("file.separator") + "ArchimedeSuper" + pc.getProperty("file.separator") + st.getCount()
				+ pc.getProperty("file.separator");

		descr.setLayout(new MigLayout());
		descr.setOpaque(false);
		JLabel descrittiva = new JLabel("Premessa descrittiva : Indicare obbiettivi e desideri del committente");
		descrittiva.setFont(new Font("Arial", Font.BOLD, 20));
		descrizioneField.setBorder(BorderFactory.createLineBorder(Color.GRAY));
		descrizioneField.setPreferredSize(new Dimension(700, 200));
		descrizioneField.setFont(new Font("Arial", Font.PLAIN, 16));
		descr.add(descrittiva, "wrap");
		descr.add(descrizioneField, "wrap");

		dati.setLayout(new MigLayout("", "[]1[]1[]", "[]5[] 7[] 7[] 7[]"));
		dati.setOpaque(false);
		setCellClima();

		photo.setLayout(new MigLayout());
		photo.setOpaque(false);
		JLabel infoUpload = new JLabel("Inserisci le tue foto : ");
		infoUpload.setFont(new Font("Arial", Font.BOLD, 20));

		upload1.setBorder(null);
		upload1.setContentAreaFilled(false);
		upload1.setRolloverEnabled(true);
		upload1.setRolloverIcon(new ImageIcon(ClassLoader.getSystemResource("upload_icon.jpg")));
		upload1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser chooser = new JFileChooser();
				ImagePreviewPanel preview = new ImagePreviewPanel();
				chooser.setAccessory(preview);
				chooser.addPropertyChangeListener(preview);
				lastPath = st.getPath();
				if (lastPath.equals(""))
					chooser.getCurrentDirectory();
				else
					chooser.setCurrentDirectory(new File(lastPath));
				chooser.showOpenDialog(upload1);
				String sourcePath = chooser.getSelectedFile().toString();

				st.setPath(sourcePath);
				lastPath = sourcePath;

				StringTokenizer tok = new StringTokenizer(sourcePath, ".");
				String sufix = "";
				while (tok.hasMoreTokens())
					sufix = tok.nextToken();

				sourceIm1 = sourcePath;
				ImageIcon tmp = new ImageIcon(sourcePath);
				tmp.setImage(tmp.getImage().getScaledInstance(250, 200, Image.SCALE_AREA_AVERAGING));

				String destPath = directoryProject + "photo1Premesse." + sufix;
				pathIm1 = destPath;
				sufixIm1 = sufix;

				upload1.setIcon(tmp);

				photo.add(upload2, "cell 1 0");
				st.refresh();
			}
		});

		upload2.setBorder(null);
		upload2.setContentAreaFilled(false);
		upload2.setRolloverEnabled(true);
		upload2.setRolloverIcon(new ImageIcon(ClassLoader.getSystemResource("upload_icon.jpg")));
		upload2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser chooser = new JFileChooser();
				ImagePreviewPanel preview = new ImagePreviewPanel();
				chooser.setAccessory(preview);
				chooser.addPropertyChangeListener(preview);
				lastPath = st.getPath();
				if (lastPath.equals(""))
					chooser.getCurrentDirectory();
				else
					chooser.setCurrentDirectory(new File(lastPath));
				chooser.showOpenDialog(upload2);
				String sourcePath = chooser.getSelectedFile().toString();
				st.setPath(sourcePath);
				lastPath = sourcePath;
				StringTokenizer tok = new StringTokenizer(sourcePath, ".");
				String sufix = "";
				while (tok.hasMoreTokens())
					sufix = tok.nextToken();

				sourceIm2 = sourcePath;
				ImageIcon tmp = new ImageIcon(sourcePath);
				tmp.setImage(tmp.getImage().getScaledInstance(250, 200, Image.SCALE_AREA_AVERAGING));

				String destPath = directoryProject + "photo2Premesse." + sufix;
				pathIm2 = destPath;
				sufixIm2 = sufix;

				upload2.setIcon(tmp);

				photo.add(upload3, "cell 2 0");

				st.refresh();
			}
		});

		upload3.setBorder(null);
		upload3.setContentAreaFilled(false);
		upload3.setRolloverEnabled(true);
		upload3.setRolloverIcon(new ImageIcon(ClassLoader.getSystemResource("upload_icon.jpg")));
		upload3.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {

				JFileChooser chooser = new JFileChooser();
				ImagePreviewPanel preview = new ImagePreviewPanel();
				chooser.setAccessory(preview);
				chooser.addPropertyChangeListener(preview);
				lastPath = st.getPath();
				if (lastPath.equals(""))
					chooser.getCurrentDirectory();
				else
					chooser.setCurrentDirectory(new File(lastPath));
				chooser.showOpenDialog(upload3);
				String sourcePath = chooser.getSelectedFile().toString();
				st.setPath(sourcePath);
				lastPath = sourcePath;
				StringTokenizer tok = new StringTokenizer(sourcePath, ".");
				String sufix = "";
				while (tok.hasMoreTokens())
					sufix = tok.nextToken();

				sourceIm3 = sourcePath;
				ImageIcon tmp = new ImageIcon(sourcePath);
				tmp.setImage(tmp.getImage().getScaledInstance(250, 200, Image.SCALE_AREA_AVERAGING));

				String destPath = directoryProject + "photo3Premesse." + sufix;
				pathIm3 = destPath;
				sufixIm3 = sufix;
				upload3.setIcon(tmp);
				photo.add(upload4, "cell 3 0");
				st.refresh();

			}
		});

		upload4.setBorder(null);
		upload4.setContentAreaFilled(false);
		upload4.setRolloverEnabled(true);
		upload4.setRolloverIcon(new ImageIcon(ClassLoader.getSystemResource("upload_icon.jpg")));
		upload4.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser chooser = new JFileChooser();
				ImagePreviewPanel preview = new ImagePreviewPanel();
				chooser.setAccessory(preview);
				chooser.addPropertyChangeListener(preview);
				lastPath = st.getPath();
				if (lastPath.equals(""))
					chooser.getCurrentDirectory();
				else
					chooser.setCurrentDirectory(new File(lastPath));
				chooser.showOpenDialog(upload4);
				String sourcePath = chooser.getSelectedFile().toString();
				st.setPath(sourcePath);
				lastPath = sourcePath;
				StringTokenizer tok = new StringTokenizer(sourcePath, ".");
				String sufix = "";
				while (tok.hasMoreTokens())
					sufix = tok.nextToken();

				sourceIm4 = sourcePath;
				ImageIcon tmp = new ImageIcon(sourcePath);
				tmp.setImage(tmp.getImage().getScaledInstance(250, 200, Image.SCALE_AREA_AVERAGING));

				String destPath = directoryProject + "photo4Premesse." + sufix;
				pathIm4 = destPath;
				sufixIm4 = sufix;

				upload4.setIcon(tmp);

				st.refresh();
			}
		});

		MouseAdapter ml1 = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3) {
					int res = JOptionPane.showConfirmDialog(null, "Eliminare foto?", "Attenzione", 0);
					if (res == 0) {
						if (!pathIm2.equals("")) {
							pathIm1 = pathIm2.replace("2P", "1P");
							sufixIm1 = sufixIm2;
							sourceIm1 = sourceIm2;
							upload1.setIcon(upload2.getIcon());

							if (!pathIm3.equals("")) {
								pathIm2 = pathIm3.replace("3P", "2P");
								sufixIm2 = sufixIm3;
								sourceIm2 = sourceIm3;
								upload2.setIcon(upload3.getIcon());
								if (!pathIm4.equals("")) {
									pathIm3 = pathIm4.replace("4P", "3P");
									sufixIm3 = sufixIm4;
									sourceIm3 = sourceIm4;
									pathIm4 = "";
									sufixIm4 = "";
									sourceIm4 = "";
									upload3.setIcon(upload4.getIcon());
									upload4.setIcon(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
								} else {
									upload3.setIcon(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
									pathIm3 = "";
									sufixIm3 = "";
									sourceIm3 = "";
									try {
										photo.remove(upload4);
									} catch (Exception noRemove) {
										System.out.println("no remove");
									}

								}
							} else {
								upload2.setIcon(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
								pathIm2 = "";
								sufixIm2 = "";
								sourceIm2 = "";
								try {
									photo.remove(upload3);
								} catch (Exception noRemove) {
									System.out.println("no remove");
								}

							}
						} else {
							pathIm1 = "";
							sufixIm1 = "";
							sourceIm1 = "";
							upload1.setIcon(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
							try {
								photo.remove(upload2);
							} catch (Exception noRemove) {
								System.out.println("no remove");
							}
						}
						st.refresh();
					}
				}
			}
		};
		upload1.addMouseListener(ml1);

		MouseAdapter ml2 = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3) {
					int res = JOptionPane.showConfirmDialog(null, "Eliminare foto?", "Attenzione", 0);
					if (res == 0) {

						if (!pathIm3.equals("")) {
							pathIm2 = pathIm3.replace("3P", "2P");
							sufixIm2 = sufixIm3;
							sourceIm2 = sourceIm3;
							upload2.setIcon(upload3.getIcon());
							if (!pathIm4.equals("")) {
								pathIm3 = pathIm4.replace("4P", "3P");
								sufixIm3 = sufixIm4;
								sourceIm3 = sourceIm4;
								pathIm4 = "";
								sufixIm4 = "";
								sourceIm4 = "";
								upload3.setIcon(upload4.getIcon());
								upload4.setIcon(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
							} else {
								upload3.setIcon(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
								pathIm3 = "";
								sufixIm3 = "";
								sourceIm3 = "";
								try {
									photo.remove(upload4);
								} catch (Exception noRemove) {
									System.out.println("no remove");
								}
							}
						} else {
							upload2.setIcon(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
							pathIm2 = "";
							sufixIm2 = "";
							sourceIm2 = "";
							try {
								photo.remove(upload3);
							} catch (Exception noRemove) {
								System.out.println("no remove");
							}

						}
						st.refresh();
					}
				}
			}
		};
		upload2.addMouseListener(ml2);

		MouseAdapter ml3 = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3) {
					int res = JOptionPane.showConfirmDialog(null, "Eliminare foto?", "Attenzione", 0);
					if (res == 0) {

						if (!pathIm4.equals("")) {
							pathIm3 = pathIm4.replace("4P", "3P");
							sufixIm3 = sufixIm4;
							sourceIm3 = sourceIm4;
							pathIm4 = "";
							sufixIm4 = "";
							sourceIm4 = "";
							upload3.setIcon(upload4.getIcon());
							upload4.setIcon(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
						} else {
							upload3.setIcon(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
							pathIm3 = "";
							sufixIm3 = "";
							sourceIm3 = "";
							try {
								photo.remove(upload4);
							} catch (Exception noRemove) {
								System.out.println("no remove");
							}
						}
						st.refresh();
					}

				}
			}
		};
		upload3.addMouseListener(ml3);

		MouseAdapter ml4 = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3) {
					int res = JOptionPane.showConfirmDialog(null, "Eliminare foto?", "Attenzione", 0);
					if (res == 0) {
						upload4.setIcon(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
						pathIm4 = "";
						sufixIm4 = "";
						sourceIm4 = "";
						st.refresh();
					}

				}
			}
		};
		upload4.addMouseListener(ml4);

		MouseAdapter formulaEsterno = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3 || e.getButton() == MouseEvent.BUTTON1) {
					try {
						umAssEsterno.setText("" + formula(tempEsterno.getText(), umidEsterno.getText()));
						paint(getGraphics());
					} catch (Exception err) {
						System.out.print("error umidità");
					}
				}
			}
		};
		umAssEsterno.addMouseListener(formulaEsterno);

		MouseAdapter formulaInterrato = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3 || e.getButton() == MouseEvent.BUTTON1) {
					try {
						umAssInterrato.setText("" + formula(tempInterrato.getText(), umidInterrato.getText()));
						paint(getGraphics());
					} catch (Exception err) {
						System.out.print("error umidità");
					}
				}
			}
		};
		umAssInterrato.addMouseListener(formulaInterrato);

		MouseAdapter formulaInterno = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3 || e.getButton() == MouseEvent.BUTTON1) {
					try {
						umAssInterno.setText("" + formula(tempInterno.getText(), umidInterno.getText()));
						paint(getGraphics());
					} catch (Exception err) {
						System.out.print("error umidità");
					}
				}
			}
		};
		umAssInterno.addMouseListener(formulaInterno);

		ClipboardPopupMenu cb = ClipboardPopupMenu.installForComponent(descrizioneField);
		cb.addCopyFunction("Copia");
		cb.addCutFunction("Taglia");
		cb.addPasteFunction("Incolla");

		ClipboardPopupMenu cbMeteo = ClipboardPopupMenu.installForComponent(condizioniMeteo);
		cbMeteo.addCopyFunction("Copia");
		cbMeteo.addCutFunction("Taglia");
		cbMeteo.addPasteFunction("Incolla");

		photo.add(infoUpload, "dock north");
		photo.add(upload1, "cell 0 0");

		add(descr, "gapleft 15 , wrap");
		add(dati, "gapleft 15 , wrap");
		add(photo, "gapleft 15 , wrap");
	}

	public void setCellClima() {

		JLabel title = new JLabel("Dati climatici ambientali");
		RoundedPanel climaAmbientale = new RoundedPanel("Clima ambientale");
		RoundedPanel temperatura = new RoundedPanel("Temperatura");
		RoundedPanel umiditRelativa = new RoundedPanel("Umidita' relativa");
		RoundedPanel umiditAss = new RoundedPanel("Umidita' assoluta");
		RoundedPanel climaEst = new RoundedPanel("Clima esterno edificio C.E.");
		RoundedPanel climaInterno = new RoundedPanel("Clima interno edificio C.I.");
		RoundedPanel climaInterr = new RoundedPanel("Clima interrato edificio C.C.");
		JLabel infoMeteo = new JLabel("<html>Condizioni meteo al momento della diagnostica e<br>periodo antecedente alla rilevazione:</html>");

		umAssEsterno.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
		umAssInterno.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
		umAssInterrato.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));

		condizioniMeteo.setFont(new Font("Arial", Font.PLAIN, 16));
		infoMeteo.setFont(new Font("Arial", Font.BOLD, 20));
		title.setFont(new Font("Arial", Font.BOLD, 20));
		umAssEsterno.setFont(new Font("Arial", Font.PLAIN, 14));
		umAssInterno.setFont(new Font("Arial", Font.PLAIN, 14));
		umAssInterrato.setFont(new Font("Arial", Font.PLAIN, 14));

		climaAmbientale.setPreferredSize(new Dimension(250, 30));
		temperatura.setPreferredSize(new Dimension(170, 30));
		umiditRelativa.setPreferredSize(new Dimension(170, 30));
		umiditAss.setPreferredSize(new Dimension(170, 30));
		climaEst.setPreferredSize(new Dimension(250, 30));
		climaInterno.setPreferredSize(new Dimension(250, 30));
		climaInterr.setPreferredSize(new Dimension(250, 30));
		umAssEsterno.setPreferredSize(new Dimension(170, 30));
		umAssInterno.setPreferredSize(new Dimension(170, 30));
		umAssInterrato.setPreferredSize(new Dimension(170, 30));
		condizioniMeteo.setPreferredSize(new Dimension(700, 70));

		climaAmbientale.setOpaque(false);

		temperatura.setOpaque(false);

		umiditRelativa.setOpaque(false);

		umiditAss.setOpaque(false);

		climaEst.setOpaque(false);

		climaInterno.setOpaque(false);

		climaInterr.setOpaque(false);

		tempEsterno.setPreferredSize(new Dimension(170, 30));
		tempInterno.setPreferredSize(new Dimension(170, 30));
		tempInterrato.setPreferredSize(new Dimension(170, 30));
		umidEsterno.setPreferredSize(new Dimension(170, 30));
		umidInterno.setPreferredSize(new Dimension(170, 30));
		umidInterrato.setPreferredSize(new Dimension(170, 30));

		tempEsterno.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
		tempInterno.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
		tempInterrato.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
		umidEsterno.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
		umidInterno.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
		umidInterrato.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));

		tempEsterno.setOpaque(false);
		tempInterno.setOpaque(false);
		tempInterrato.setOpaque(false);
		umidEsterno.setOpaque(false);
		umidInterno.setOpaque(false);
		umidInterrato.setOpaque(false);

		dati.add(title, "dock north");
		dati.add(climaAmbientale, "cell 0 0");
		dati.add(temperatura, "cell 1 0");
		dati.add(umiditRelativa, "cell 2 0");
		dati.add(umiditAss, "cell 3 0");
		// dati.add(climaInterno, "cell 0 1");
		// dati.add(tempInterno, "cell 1 1");
		// dati.add(umidInterno, "cell 2 1");
		// dati.add(umAssInterno, "cell 3 1");
		// dati.add(climaEst, "cell 0 2");
		// dati.add(tempEsterno, "cell 1 2");
		// dati.add(umidEsterno, "cell 2 2");
		// dati.add(umAssEsterno, "cell 3 2");
		// dati.add(climaInterr, "cell 0 3");
		// dati.add(tempInterrato, "cell 1 3");
		// dati.add(umidInterrato, "cell 2 3");
		// dati.add(umAssInterrato, "cell 3 3 , wrap");

		dati.add(climaEst, "cell 0 1");
		dati.add(tempEsterno, "cell 1 1");
		dati.add(umidEsterno, "cell 2 1");
		dati.add(umAssEsterno, "cell 3 1");
		dati.add(climaInterr, "cell 0 2");
		dati.add(tempInterrato, "cell 1 2");
		dati.add(umidInterrato, "cell 2 2");
		dati.add(umAssInterrato, "cell 3 2");
		dati.add(climaInterno, "cell 0 3");
		dati.add(tempInterno, "cell 1 3");
		dati.add(umidInterno, "cell 2 3");
		dati.add(umAssInterno, "cell 3 3 , wrap");

		JPanel meteo = new JPanel();
		meteo.setLayout(new MigLayout());
		meteo.setOpaque(false);
		meteo.add(infoMeteo, "wrap");
		meteo.add(condizioniMeteo);

		dati.add(meteo, "span , wrap");

	}

	public String print() {
		String print = "";
		print = print + "\n#START\n#" + descrizioneField.getText() + "\n#STOP";
		print = print + "\n#" + tempEsterno.getText();
		print = print + "\n#" + umidEsterno.getText();
		print = print + "\n#" + tempInterno.getText();
		print = print + "\n#" + umidInterno.getText();
		print = print + "\n#" + tempInterrato.getText();
		print = print + "\n#" + umidInterrato.getText();
		print = print + "\n#" + umAssEsterno.getText();
		print = print + "\n#" + umAssInterno.getText();
		print = print + "\n#" + umAssInterrato.getText();
		print = print + "\n#" + condizioniMeteo.getText();
		print = print + "\n#meteoStop";

		print = print + "\n#" + pathIm1;
		print = print + "\n#" + pathIm2;
		print = print + "\n#" + pathIm3;
		print = print + "\n#" + pathIm4;
		print = print + "\n@@Premesse";
		save();
		return print;
	}

	public void setElement(String data) {
		Scanner c = new Scanner(data);
		c.nextLine();
		// start line
		c.nextLine().substring(1);
		String tmp = c.nextLine().substring(1);
		String premesse = "";
		for (int i = 1; i > 0;) {

			// System.out.println("^^^" + tmp);
			if (tmp.equalsIgnoreCase("#stop")) {
				descrizioneField.setText(premesse.substring(0, premesse.length() - 1));
				i = -1;
				break;
			}
			premesse = premesse + tmp + "\n";
			tmp = c.nextLine();
		}
		tempEsterno.setText(c.nextLine().substring(1));
		umidEsterno.setText(c.nextLine().substring(1));
		tempInterno.setText(c.nextLine().substring(1));
		umidInterno.setText(c.nextLine().substring(1));
		tempInterrato.setText(c.nextLine().substring(1));
		umidInterrato.setText(c.nextLine().substring(1));
		umAssEsterno.setText(c.nextLine().substring(1));
		umAssInterno.setText(c.nextLine().substring(1));
		umAssInterrato.setText(c.nextLine().substring(1));

		tmp = c.nextLine().substring(1);
		for (int i = 1; i > 0;) {
			String to = c.nextLine();
			if (to.equals("#meteoStop"))
				i = -1;
			else
				tmp = tmp + "\n" + to;

		}
		condizioniMeteo.setText(tmp);

		pathIm1 = c.nextLine().substring(1);
		if (pathIm1.contains("Premesse")) {
			pathIm1 = directoryProject + "photo1Premesse." + pathIm1.substring(pathIm1.indexOf(".") + 1);
			upload1.setIcon(new ImageIcon(pathIm1));
			photo.add(upload2, "cell 1 0");
		} else
			pathIm1 = "";
		pathIm2 = c.nextLine().substring(1);
		if (pathIm2.contains("Premesse")) {
			pathIm2 = directoryProject + "photo2Premesse." + pathIm2.substring(pathIm2.indexOf(".") + 1);
			upload2.setIcon(new ImageIcon(pathIm2));
			photo.add(upload3, "cell 2 0");
		} else
			pathIm2 = "";
		pathIm3 = c.nextLine().substring(1);
		if (pathIm3.contains("Premesse")) {
			pathIm3 = directoryProject + "photo3Premesse." + pathIm3.substring(pathIm3.indexOf(".") + 1);
			upload3.setIcon(new ImageIcon(pathIm3));
			photo.add(upload4, "cell 3 0");
		} else
			pathIm3 = "";
		pathIm4 = c.nextLine().substring(1);
		if (pathIm4.contains("Premesse")) {
			pathIm4 = directoryProject + "photo4Premesse." + pathIm4.substring(pathIm4.indexOf(".") + 1);
			upload4.setIcon(new ImageIcon(pathIm4));
		} else
			pathIm4 = "";
		paintAll(getGraphics());
	}

	public void resize(File in, int w, int h, String formato, String out) throws IOException {
		Image newImg = ImageIO.read(in).getScaledInstance(w, h, Image.SCALE_SMOOTH);
		BufferedImage bim = new java.awt.image.BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
		bim.createGraphics().drawImage(newImg, 0, 0, null);
		FileOutputStream fos = new FileOutputStream(out);
		ImageIO.write(bim, formato, fos);
		fos.close();
	}

	public void save() {

		try {
			if (!sourceIm1.equals(""))
				resize(new File(sourceIm1), 250, 200, sufixIm1, pathIm1);
			if (!sourceIm2.equals(""))
				resize(new File(sourceIm2), 250, 200, sufixIm2, pathIm2);
			if (!sourceIm3.equals(""))
				resize(new File(sourceIm3), 250, 200, sufixIm3, pathIm3);
			if (!sourceIm4.equals(""))
				resize(new File(sourceIm4), 250, 200, sufixIm4, pathIm4);
		} catch (IOException e) {
			System.out.println("no resize");
			e.printStackTrace();

		}
	}

	public Document PDF(Document doc) {
		int col = 0;

		if (!pathIm1.equals(""))
			col++;
		if (!pathIm2.equals(""))
			col++;
		if (!pathIm3.equals(""))
			col++;
		if (!pathIm4.equals(""))
			col++;

		// foto
		PdfPTable table = new PdfPTable(2);
		table.setWidthPercentage(100);
		if (!pathIm1.equals(""))
			try {
				com.itextpdf.text.Image img = com.itextpdf.text.Image.getInstance(pathIm1);
				img.setCompressionLevel(5);
				PdfPCell cos = new PdfPCell(img);
				if (col < 2) {
					img.scaleAbsolute(250f, 200f);
					cos.setColspan(2);
				} else
					img.scaleAbsolute(235f, 165f);
				cos.setHorizontalAlignment(Element.ALIGN_CENTER);
				cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
				cos.setBorder(PdfPCell.NO_BORDER);
				table.addCell(cos);
			} catch (BadElementException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		if (!pathIm2.equals(""))
			try {
				com.itextpdf.text.Image img = com.itextpdf.text.Image.getInstance(pathIm2);
				img.setCompressionLevel(5);
				if (col < 2)
					img.scaleAbsolute(250f, 200f);
				else
					img.scaleAbsolute(235f, 165f);
				PdfPCell cos = new PdfPCell(img);
				cos.setHorizontalAlignment(Element.ALIGN_CENTER);
				cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
				cos.setBorder(PdfPCell.NO_BORDER);
				table.addCell(cos);
			} catch (BadElementException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		if (!pathIm3.equals(""))
			try {
				com.itextpdf.text.Image img = com.itextpdf.text.Image.getInstance(pathIm3);
				img.setCompressionLevel(5);
				Paragraph dati;
				dati = new Paragraph("");
				PdfPCell space = new PdfPCell(dati);
				space.setBorder(PdfPCell.NO_BORDER);
				table.addCell(space);
				table.addCell(space);

				PdfPCell cos = new PdfPCell(img);
				if (col < 2)
					img.scaleAbsolute(250f, 200f);
				else
					img.scaleAbsolute(235f, 165f);
				if (col == 3)
					cos.setColspan(2);
				cos.setHorizontalAlignment(Element.ALIGN_CENTER);
				cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
				cos.setBorder(PdfPCell.NO_BORDER);
				table.addCell(cos);
			} catch (BadElementException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		if (!pathIm4.equals(""))
			try {
				com.itextpdf.text.Image img = com.itextpdf.text.Image.getInstance(pathIm4);
				img.setCompressionLevel(5);
				PdfPCell cos = new PdfPCell(img);
				if (col < 2)
					img.scaleAbsolute(250f, 200f);
				else
					img.scaleAbsolute(235f, 165f);
				cos.setHorizontalAlignment(Element.ALIGN_CENTER);
				cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
				cos.setBorder(PdfPCell.NO_BORDER);
				table.addCell(cos);
			} catch (BadElementException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		try {
			doc.add(table);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// descrizione
		Paragraph dati;
		dati = new Paragraph(descrizioneField.getText(), FontFactory.getFont(FontFactory.TIMES, 11));
		try {
			dati.setSpacingBefore(10);
			if (col > 2)
				dati.setSpacingAfter(20);
			else
				dati.setSpacingAfter(150);
			doc.add(dati);
		} catch (DocumentException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}

		// clima
		Chunk underline = new Chunk("2. Dati climatici ambientali", FontFactory.getFont(FontFactory.TIMES, 15, Font.BOLD));
		underline.setUnderline(1.2f, -2f); // 0.1 thick, -2 y-location

		try {
			doc.add(underline);
		} catch (DocumentException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}

		table = new PdfPTable(4);
		table.setWidthPercentage(100);
		Phrase testo;
		PdfPCell cos;
		// line1
		testo = new Phrase("Clima ambientale", FontFactory.getFont(FontFactory.TIMES, 11, Font.BOLD));
		cos = new PdfPCell(testo);
		cos.setHorizontalAlignment(Element.ALIGN_CENTER);
		cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cos.setFixedHeight(20);
		table.addCell(cos);

		testo = new Phrase("Temperatura", FontFactory.getFont(FontFactory.TIMES, 11, Font.BOLD));
		cos = new PdfPCell(testo);
		cos.setHorizontalAlignment(Element.ALIGN_CENTER);
		cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cos.setFixedHeight(20);
		table.addCell(cos);

		testo = new Phrase("Umidità relativa", FontFactory.getFont(FontFactory.TIMES, 11, Font.BOLD));
		cos = new PdfPCell(testo);
		cos.setHorizontalAlignment(Element.ALIGN_CENTER);
		cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cos.setFixedHeight(20);
		table.addCell(cos);

		testo = new Phrase("Umidità assoluta", FontFactory.getFont(FontFactory.TIMES, 11, Font.BOLD));
		cos = new PdfPCell(testo);
		cos.setHorizontalAlignment(Element.ALIGN_CENTER);
		cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cos.setFixedHeight(20);
		table.addCell(cos);

		// line2 esterno
		testo = new Phrase("Clima esterno", FontFactory.getFont(FontFactory.TIMES, 11, Font.BOLD));
		cos = new PdfPCell(testo);
		cos.setHorizontalAlignment(Element.ALIGN_CENTER);
		cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cos.setFixedHeight(20);
		table.addCell(cos);

		testo = new Phrase(tempEsterno.getText() + "°", FontFactory.getFont(FontFactory.TIMES, 11));
		cos = new PdfPCell(testo);
		cos.setHorizontalAlignment(Element.ALIGN_CENTER);
		cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cos.setFixedHeight(20);
		table.addCell(cos);

		testo = new Phrase(umidEsterno.getText() + "%", FontFactory.getFont(FontFactory.TIMES, 11));
		cos = new PdfPCell(testo);
		cos.setHorizontalAlignment(Element.ALIGN_CENTER);
		cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cos.setFixedHeight(20);
		table.addCell(cos);

		testo = new Phrase(umAssEsterno.getText() + "%", FontFactory.getFont(FontFactory.TIMES, 11));
		cos = new PdfPCell(testo);
		cos.setHorizontalAlignment(Element.ALIGN_CENTER);
		cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cos.setFixedHeight(20);
		table.addCell(cos);

		// line3 interrato
		testo = new Phrase("Clima interrato", FontFactory.getFont(FontFactory.TIMES, 11, Font.BOLD));
		cos = new PdfPCell(testo);
		cos.setHorizontalAlignment(Element.ALIGN_CENTER);
		cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cos.setFixedHeight(20);
		table.addCell(cos);

		testo = new Phrase(tempInterrato.getText() + "°", FontFactory.getFont(FontFactory.TIMES, 11));
		cos = new PdfPCell(testo);
		cos.setHorizontalAlignment(Element.ALIGN_CENTER);
		cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cos.setFixedHeight(20);
		table.addCell(cos);

		testo = new Phrase(umidInterrato.getText() + "%", FontFactory.getFont(FontFactory.TIMES, 11));
		cos = new PdfPCell(testo);
		cos.setHorizontalAlignment(Element.ALIGN_CENTER);
		cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cos.setFixedHeight(20);
		table.addCell(cos);

		testo = new Phrase(umAssInterrato.getText() + "%", FontFactory.getFont(FontFactory.TIMES, 11));
		cos = new PdfPCell(testo);
		cos.setHorizontalAlignment(Element.ALIGN_CENTER);
		cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cos.setFixedHeight(20);
		table.addCell(cos);

		// line4 interno
		testo = new Phrase("Clima interno", FontFactory.getFont(FontFactory.TIMES, 11, Font.BOLD));
		cos = new PdfPCell(testo);
		cos.setHorizontalAlignment(Element.ALIGN_CENTER);
		cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cos.setFixedHeight(20);
		table.addCell(cos);

		testo = new Phrase(tempInterno.getText() + "°", FontFactory.getFont(FontFactory.TIMES, 11));
		cos = new PdfPCell(testo);
		cos.setHorizontalAlignment(Element.ALIGN_CENTER);
		cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cos.setFixedHeight(20);
		table.addCell(cos);

		testo = new Phrase(umidInterno.getText() + "%", FontFactory.getFont(FontFactory.TIMES, 11));
		cos = new PdfPCell(testo);
		cos.setHorizontalAlignment(Element.ALIGN_CENTER);
		cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cos.setFixedHeight(20);
		table.addCell(cos);

		testo = new Phrase(umAssInterno.getText() + "%", FontFactory.getFont(FontFactory.TIMES, 11));
		cos = new PdfPCell(testo);
		cos.setHorizontalAlignment(Element.ALIGN_CENTER);
		cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cos.setFixedHeight(20);
		table.addCell(cos);

		// line4 meteo generico
		testo = new Phrase("Condizioni meteo al momento della diagnostica ", FontFactory.getFont(FontFactory.TIMES, 11, Font.BOLD));
		cos = new PdfPCell(testo);
		cos.setHorizontalAlignment(Element.ALIGN_CENTER);
		cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
		// cos.setFixedHeight(22);
		cos.setColspan(2);
		cos.disableBorderSide(PdfPCell.RIGHT);
		table.addCell(cos);

		testo = new Phrase(condizioniMeteo.getText(), FontFactory.getFont(FontFactory.TIMES, 11));
		cos = new PdfPCell(testo);
		cos.setHorizontalAlignment(Element.ALIGN_CENTER);
		cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
		// cos.setFixedHeight(22);
		cos.disableBorderSide(PdfPCell.LEFT);
		cos.setColspan(2);
		table.addCell(cos);

		try {
			doc.add(table);
		} catch (DocumentException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		return doc;
	}

	public String formula(String temp, String umidita) {
		int t = Integer.parseInt(temp);
		int u = Integer.parseInt(umidita);
		// System.out.println("t "+t+" u"+u);
		return String.format("%.3g", tabella[t + 5] * u / 100);
	}

}
